package zero.tech.games.common;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import zero.tech.games.common.data.RoomNode;
import zero.tech.redis.cache.RedisUtil;
@Component
public class RoomNodeUtil {
	@Autowired
	private RedisUtil redisUtil;
	@Autowired
	private RoomIdUtil roomIdUtil;
	
	private static final Map<String,Map<Integer,Map<String,Map<String,RoomNode>>>> secondCache=new HashMap<>();
	
	public RoomNode getRoomNode(String club_id, String room_id) {
		return (RoomNode) redisUtil.hget(RedisConsts.B_CLUB_ROOMNODE,club_id+"###"+room_id);
	}

	public void insertOrUpdate(RoomNode roomNode) {
		String room_id=roomNode.getRoom_id();
		String club_id=roomNode.getClub_id();
		redisUtil.hset(RedisConsts.B_CLUB_ROOMNODE,club_id+"###"+room_id, roomNode);		
	}
	
	public void insertOrUpdateBatch(RoomNode... roomNodes) {
		if(roomNodes.length>0) {
			Map<Object,Object> map=new HashMap<>();
			for(RoomNode roomNode:roomNodes) {
				String room_id=roomNode.getRoom_id();
				String club_id=roomNode.getClub_id();
				String key=club_id+"###"+room_id;
				map.put(key, roomNode);
			}
			if(!map.isEmpty()) {
				redisUtil.hmset(RedisConsts.B_CLUB_ROOMNODE,map);		
			}
		}
	}
	
	public void delete(String room_id,String club_id) {
		redisUtil.hdel(RedisConsts.B_CLUB_ROOMNODE,club_id+"###"+room_id);
		//清理房间唯一id
		roomIdUtil.deleteRoomId(room_id);
		System.out.println("房间节点信息被清理了");
	}
	
	public void deleteBatch(RoomNode...roomNodes) {
		if(roomNodes.length>0) {
			Object[] keys=new Object[roomNodes.length];
			Object[] room_ids=new Object[roomNodes.length];
			int index=0;
			int j=0;
			for(RoomNode roomNode:roomNodes) {
				keys[index++]=roomNode.getClub_id()+"###"+roomNode.getRoom_id();
				room_ids[j++]=roomNode.getRoom_id();
			}
			redisUtil.hdel(RedisConsts.B_CLUB_ROOMNODE, keys);
			roomIdUtil.deleteRoomIdBatch(room_ids);
			System.out.println("房间节点信息被清理了");
		}
	}
	
	public List<RoomNode> getAllRooms() {
		Map<Object, Object> result=redisUtil.hgetAll(RedisConsts.B_CLUB_ROOMNODE);		
		List<RoomNode> list=new ArrayList<RoomNode>();
		for(Object obj:result.values()) {
			RoomNode tmp=(RoomNode) obj;
			list.add(tmp);
		}
		return list;
	}

	
	public Map<Integer,Map<String,Map<String,RoomNode>>> getSecondCacheRoomsByClubId(String club_id) {
		Map<Integer,Map<String,Map<String,RoomNode>>> roomNodes=new HashMap<>();
		if(secondCache.containsKey(club_id)) {
			Map<Integer,Map<String,Map<String,RoomNode>>> map=secondCache.get(club_id);
			roomNodes.putAll(map);
		}
		return roomNodes;
	}

	public void timer() {
		List<RoomNode> list=getAllRooms();
		Map<String,Map<Integer,Map<String,Map<String,RoomNode>>>> map=new HashMap<>();
		for(RoomNode roomNode:list) {
			String club_id=roomNode.getClub_id();
			int game_id=roomNode.getGame_id();
			String pipei_key=roomNode.getPipei_key();
			String room_id=roomNode.getRoom_id();
			putInClubMap(map,club_id,game_id,pipei_key,room_id,roomNode);
		}
		synchronized (secondCache) {
			secondCache.clear();
			secondCache.putAll(map);
		}
	}

	private void putInClubMap(Map<String, Map<Integer, Map<String, Map<String, RoomNode>>>> map, String club_id,
			int game_id, String pipei_key, String room_id, RoomNode roomNode) {
		if(!map.containsKey(club_id)) {
			Map<Integer,Map<String,Map<String,RoomNode>>> mmap=new HashMap<>();
			putInGameMap(mmap,game_id,pipei_key,room_id,roomNode);
			map.put(club_id, mmap);
		}else {
			Map<Integer,Map<String,Map<String,RoomNode>>> mmap=map.get(club_id);
			putInGameMap(mmap,game_id,pipei_key,room_id,roomNode);
		}		
	}

	private void putInGameMap(Map<Integer, Map<String, Map<String, RoomNode>>> mmap, int game_id, String pipei_key, String room_id, RoomNode roomNode) {
		if(!mmap.containsKey(game_id)) {
			Map<String,Map<String,RoomNode>> mmmap=new HashMap<>();
			putInPipeiMap(mmmap,pipei_key,room_id,roomNode);
			mmap.put(game_id, mmmap);
		}else {
			Map<String,Map<String,RoomNode>> mmmap=mmap.get(game_id);
			putInPipeiMap(mmmap,pipei_key,room_id,roomNode);
		}		
	}

	private void putInPipeiMap(Map<String, Map<String, RoomNode>> mmmap,String pipei_key,String room_id,RoomNode roomNode) {
		if(!mmmap.containsKey(pipei_key)) {
			Map<String,RoomNode> mmmmap=new HashMap<>();
			mmmmap.put(room_id, roomNode);
			mmmap.put(pipei_key, mmmmap);
		}else {
			Map<String,RoomNode> mmmmap=mmmap.get(pipei_key);
			mmmmap.put(room_id, roomNode);
		}		
	}
}
